/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.editors;

import faneclipse.Activator;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

/**
 * The FanSourceViewerConfiguration class is in charge of the configuration of the display of the text.
 * @author BEGAUDEAU stephane
 */
public class FanSourceViewerConfiguration extends SourceViewerConfiguration {
	
	/**
	 * The constructor.
	 */
	public FanSourceViewerConfiguration() {
		super();
	}
	
	/**
	 * @param sourceViewer The source viewer.
	 * @return An identifier for the partitioning.
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
	 */
	public final String getConfiguredDocumentPartitioning(final ISourceViewer sourceViewer) {
		return Activator.FAN_PARTITIONING;
	}
	
	/**
	 * @param sourceViewer The source viewer.
	 * @return Return the differents types of content/partition of the document.
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(org.eclipse.jface.text.source.ISourceViewer)
	 */
	public final String[] getConfiguredContentTypes(final ISourceViewer sourceViewer) {
		String[] configuredContentTypes = {IDocument.DEFAULT_CONTENT_TYPE};
		return configuredContentTypes;
	}
	
	/**
	 * Initialize all the damagers and the repairers needed for the document.
	 * @param sourceViewer The sourceviewer containing the partitioning for fan files.
	 * @return A IPresentationReconciler in charge of maintaining the presentation of the document.
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
	 */
	public final IPresentationReconciler getPresentationReconciler(final ISourceViewer sourceViewer) {
		FanTextAttributeProvider provider= FanEditor.getFanTextAttributeProvider();
		PresentationReconciler reconciler= new PresentationReconciler();
		reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
		
		// Damager & repairer for default content
		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(FanEditor.getFanCodeScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
		
		// Damager & repairer for the comments
		DefaultDamagerRepairer drCommentSingleLine = new DefaultDamagerRepairer(new FanSingleTokenScanner(provider.getAttribute(FanTextAttributeProvider.COMMENT_ATTRIBUTE)));
		reconciler.setDamager(drCommentSingleLine, FanPartitionScanner.FAN_COMMENT_SINGLE_LINE);
		reconciler.setRepairer(drCommentSingleLine, FanPartitionScanner.FAN_COMMENT_SINGLE_LINE);
		
		DefaultDamagerRepairer drCommentMultiLine = new DefaultDamagerRepairer(new FanSingleTokenScanner(provider.getAttribute(FanTextAttributeProvider.COMMENT_ATTRIBUTE)));
		reconciler.setDamager(drCommentMultiLine, FanPartitionScanner.FAN_COMMENT_MULTI_LINE);
		reconciler.setRepairer(drCommentMultiLine, FanPartitionScanner.FAN_COMMENT_MULTI_LINE);
		
		DefaultDamagerRepairer drFanDoc = new DefaultDamagerRepairer(new FanSingleTokenScanner(provider.getAttribute(FanTextAttributeProvider.COMMENT_ATTRIBUTE)));
		reconciler.setDamager(drFanDoc, FanPartitionScanner.FAN_COMMENT_FAN_DOC);
		reconciler.setRepairer(drFanDoc, FanPartitionScanner.FAN_COMMENT_FAN_DOC);
		
		return reconciler;
	}
}
